#include <tsinide/TsinConfigDialog.hpp>
#include <ui_TsinConfigDialog.h>

#include <QtGui>
#include <QSettings>
#include <QDebug>

TsinConfigDialog::TsinConfigDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::TsinConfigDialog)
{
        ui->setupUi(this);
        fillSettings();

        connect(this, SIGNAL(accepted()), this, SLOT(updateSettings()));
}

TsinConfigDialog::~TsinConfigDialog()
{
        delete ui;
}

void TsinConfigDialog::updateSettings()
{
        QSettings settings;
        settings.setValue("tsin/path", ui->tsinPathText->text());
        settings.setValue("tsin/loglvl", ui->logLvlBox->currentIndex());
        settings.setValue("tsin/cmdline", ui->cmdlineText->text());
}

void TsinConfigDialog::fillSettings()
{
        QSettings settings;

        QString path = settings.value("tsin/path", "tsin.exe").toString();
        int loglvl = settings.value("tsin/loglvl", 4).toInt();
        QString cmdline = settings.value("tsin/cmdline", "").toString();

        ui->tsinPathText->setText(path);
        ui->logLvlBox->setCurrentIndex(loglvl);
        ui->cmdlineText->setText(cmdline);
}

void TsinConfigDialog::on_tsinPathButton_clicked()
{
        QString path = QFileDialog::getOpenFileName(this, tr("Locate tsin.exe"),
                                                        QString(),
                                                        tr("tsin executable (tsin.exe)"));
        if (!path.isEmpty())
                ui->tsinPathText->setText(path);
}
